home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 / Ham Radio 2000.iso / ham2000 / misc / kfactor / kfactor.c < prev    next >
C/C++ Source or Header  |  1993-08-13  |  4KB  |  138 lines

  1. /* kfactor.c
  2.  * Calculate stability factor K. Mainly used to analyse the results
  3.  * of the famous PUFF program, as this is missing stability calculations.
  4.  *
  5.  * Stability can be expressed in terms of S-parameters.
  6.  * The concept of stability can be expressed as:
  7.  *
  8.  *                   2        2    2
  9.  *          1 - |S11|  - |S22|  + D
  10.  *      K = ---------------------------
  11.  *              2 * |S12 * S21|
  12.  *
  13.  *      where D = |S11 * S22 - S12 * S21|
  14.  *
  15.  * Definition: A linear 2-port is unconditional stable if K > 1 and D < 1.
  16.  *
  17.  * 7-Aug-1992
  18.  * Peter Beyer  -PA3AEF-
  19.  * beyer@athena.uto.dec.com
  20.  */
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <math.h>
  24. #include <string.h>
  25.  
  26. /* Definitions */
  27. #ifndef __TURBOC__
  28. struct complex {
  29.     double x;                       /* Real part */
  30.     double y;                       /* Imaginairy part */
  31. };
  32. #endif
  33.  
  34. typedef struct _PUFF {
  35.         float f;
  36.     struct complex *sp[4];      /* Order here must be S11 S22 S12 S21 !!!*/
  37.         float K;
  38. } PUFF;
  39.  
  40. /* Prototyping */
  41. int stable(PUFF *p);
  42.  
  43. /* Operating system entry point */
  44. main(argc, argv)
  45. int argc;
  46. char **argv;
  47. {
  48.     FILE *fp;
  49.     char file[50];
  50.     char line[150];                             /* Line to read in */
  51.     struct complex spars[4] = {0, NULL, NULL, NULL, NULL, 0, 0};
  52.     PUFF p;
  53.     int i, j;
  54.     char *sp[] = {{"S11"}, {"S22"}, {"S12"}, {"S21"}};
  55.     int nspars = 0;
  56.     char tmp[5][4];
  57.  
  58.     /* Check command line arguments */
  59.     if (argc < 2) {
  60.     printf("kfactor - calculate stability from PUFF output (c) PA3AEF\n");
  61.     printf("Enter PUFF output file: ");
  62.     gets(file);
  63.     } else {
  64.     strcpy(file, argv[1]);
  65.     }
  66.  
  67.     /* Open .PUF file */
  68.     if (!strstr(strupr(file), ".PUF"))
  69.         strcat(file, ".PUF");
  70.     if ((fp = fopen(file, "r")) == NULL) {
  71.         printf("\nError opening %s\n",file);
  72.         exit(0);
  73.     }
  74.  
  75.     /* Find begin of S-parameter part in .PUF file */
  76.     while (fgets(line, 150, fp) != NULL) {
  77.     if ((strstr(line, "s{parameters}")) != NULL)
  78.             break;
  79.     }
  80.  
  81.     /* First line shows us the sequence of S-parameters */
  82.     fgets(line, 150, fp);
  83.     sscanf(&line, "%s %s %s %s %s", tmp[0], tmp[1], tmp[2], tmp[3], tmp[4]);
  84.     /* Get the sequence right for our stability routine */
  85.     for (i=0; i<4; i++) {
  86.         for (j=0; j<4; j++) {
  87.         if ((strcmp(sp[j], tmp[i+1])) == 0) {
  88.                 p.sp[i] = &spars[j];
  89.                 nspars++;
  90.                 break;
  91.             }
  92.         }
  93.     }
  94.  
  95.     /* Check mis-usage */
  96.     if (nspars != 4) {
  97.         printf("%s is missing! Need S11 S22 S12 S21.\n", sp[nspars]);
  98.         exit(0);
  99.     }
  100.  
  101.     /* Now for each line calculate what we want */
  102.     while (fgets(line, 150, fp) != NULL) {
  103.     if ((strstr(line, "{")) !=NULL)
  104.         break;
  105.     sscanf(&line,"%f %le %le %le %le %le %le %le %le",
  106.         &p.f,
  107.         &(spars[0].x), &(spars[0].y),
  108.         &(spars[1].x), &(spars[1].y),
  109.         &(spars[2].x), &(spars[2].y),
  110.         &(spars[3].x), &(spars[3].y));
  111.     if (!(stable(&p)))
  112.         printf("%7.3f GHz\tK = %le dB\tNot stable\n",p.f, p.K);
  113.     else
  114.         printf("%7.3f GHz\tK = %le dB\tStable\n",p.f, p.K);
  115.     }
  116. }
  117.  
  118. /* Calculate Rollet's K-factor from S-parameters */
  119. int stable(p)
  120. PUFF *p;
  121. {
  122.     double D;
  123.     struct complex t1, t2, t3;
  124.  
  125.     /* Calculate D */
  126.  
  127.     D = (p->sp[0]->x * p->sp[1]->x) - (p->sp[2]->x * p->sp[3]->x);
  128.  
  129.     /* Full K calculation */
  130.     p->K = (1 +pow(D, 2) - pow(p->sp[0]->x, 2) - pow(p->sp[1]->x, 2)) /
  131.         (2 * p->sp[2]->x * p->sp[3]->x);
  132.  
  133.     /* Are we stable ? */
  134.     if ((p->K < 1) || (D > 1))
  135.         return(0);
  136.     return(1);
  137. }
  138.